package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Focus;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.FocusMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.IFocusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author saber
 * @date 2018/11/13  17:12
 * @description:
 */
@Service
public class FocusServiceImpl implements IFocusService {

    @Autowired
    private FocusMapper focusMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveFocus(Long focusId, Long focusedId) {
        User focusUser = userMapper.selectByPrimaryKey(focusId);
        User focusedUser = userMapper.selectByPrimaryKey(focusedId);
        Focus focus = new Focus();
        focus.setFocus(focusUser);
        focus.setFocused(focusedUser);
        focusMapper.insert(focus);
    }

    @Override
    public void get(Long focusId, Long focusedId) {
        Focus focus = focusMapper.selectByFocusIdAndFocusedId(focusId, focusedId);
        if (focus != null) {
            throw new RuntimeException("你已经关注了当前作者");
        }
    }

    @Override
    public Map getCount(Long focusId) {
        Map map = new HashMap(16);
        Integer focusCount = focusMapper.getFocusCount(focusId);
        Integer focusedCount = focusMapper.getFocusedCount(focusId);
        map.put("focus",focusCount);
        map.put("focused",focusedCount);
        return map;
    }

    @Override
    public void deleteFocus(Long focusId, Long focusedId) {
        focusMapper.deleteFocus(focusId,focusedId);
    }
}
